/**
 * 
 */
package present;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import candy.SortedByPrice;
import candy.Sweet;

/**
 * @author Nugumanova V.
 * 
 */
@SuppressWarnings("serial")
public class Present implements Serializable {
	private List<Sweet> sweetList = new ArrayList<Sweet>();
	private double totalWeight;
	private double maxPrice;

	public Present() {
	}

	public Present(double maxPrice) {
		this.maxPrice = maxPrice;
	}

	public boolean addSweet(Sweet sweet) {
		double pricePresent = 0;
		for (Sweet candy : sweetList) {
			pricePresent = pricePresent + candy.getPrice();
		}
		if ((pricePresent = pricePresent + sweet.getPrice()) <= maxPrice) {
			sweetList.add(sweet);
			this.totalWeight = this.totalWeight + sweet.getWeight();
			return true;
		} else {
			return false;
		}
	}

	public void sortByPrice() {
		Collections.sort(sweetList, new SortedByPrice());
	}

	public List<Sweet> getRangeCarbohydrates(double minCarbohydrates,
			double maxCarbohydrates) {
		List<Sweet> range = new ArrayList<Sweet>();
		for (Sweet sweet : sweetList) {
			if ((sweet.getCarbohydrates() >= minCarbohydrates)
					& (sweet.getCarbohydrates() <= maxCarbohydrates))
				range.add(sweet);
		}
		return range;
	}

	public double getTotalWeight() {
		return totalWeight;
	}

	@Override
	public String toString() {
		String s = "";
		for (Sweet sweet : sweetList) {
			s = s + sweet.toString() + "\n" + "----------" + "\n";

		}
		return s;
	}

}
